package com.lijy.superseckill.controller;

import com.lijy.superseckill.domain.User;
import com.lijy.superseckill.repository.UserRepository;
import com.lijy.superseckill.result.Result;
import com.lijy.superseckill.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserService userService;

    @PostMapping("/person/save")
    public User save(@RequestParam String name) {
        User u = new User();
        u.setName(name);
        if (userRepository.save(u)) {
            System.out.printf("用户对象：% 保存成功！", u);
        }
        return u;
    }

    @GetMapping("/person/get")
    public Result<User> get() {
        User user = userService.getById(1);
        return Result.ok(user);
    }

    @GetMapping("/person/insert")
    public Result insert() {
        Boolean flag = userService.insert();
        return Result.ok(flag);
    }

    @RequestMapping("/getUser")
    @Cacheable(value="user-key")
    public User getUser() {
        User user = userService.getById(1);
        System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
        return user;
    }

}
